package com.huangcy.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.huangcy.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    @Value("${alipay.app_id}")
    private String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDqSDWdMzxm6stJXPCRSnkoPXWWg+oyucWu/32YbB1YYreayF1KjrHVLAQ+nDXniAgU3cEpDh0KBrv4gqMtobCeaTKpYPjTjfKi5ux/qrKYOdQCX+lsNQnxoCtzaPMeB3HgzQRWv9zYC4zpwuPh3shP91IEXreR3IkW1kQYtXUQUjc+2AdFUcND0qnesjcxJbWjPtVteXM9LkhTqIqsOopcHLhASl2y3DKaWtBMx2nzBjuTM55DnFgoYINJXgEkehTrIY7pCBddy/BrZiQ8dLh5ZKI9R2pAAkPx9VhdgIXph0fUE6RwXar7EYPzs9b873GSO2u7GoALitrxmU5GhS8hAgMBAAECggEBAMvV36/SMC9rAUvnLPd8UEWOk4xNYaqYr0I+0EuZZT7NROnKgkCoPlk2XssKpmu3kzB0b+cEfIeIrGrfXpIJJ93rsURlNpj5262YNRQutAx149vdDv4yd2Be9u8G1N+/onjwZ2ICtGbURdns0tBmQOoySI28ywKBWlUgP0cwOZXkCCn3v9sTEXalW2nR39jNtxWSZxiD/jqrVZacNIK8cdQcs8APfCdp3hX6yx1Ngp+Pwyh+ef6xqbmetjfE8BSYG3rOODTuJFbgVfRWgMBQiUE1kgMue6u4f7Lit/qA6carod9SB8mBKFm5bSEIh9GkY4MSJqaOc+/2IuAB/ad0U0ECgYEA+ZXxOHHQvP+AoifVpmo/npHljbAEvyz1Kq8ydbnWq+Qi2v1KzNRAq6VdCHMdguqIbARWbgDkZJMIJ5sJs9ra5g8M384KFOrDUv7kzRCnYlzQSEmSdllbQ5slcu8wH3ps2EMIb7lkREtkrTjkzAsVpCAVKMZDUjFoO5wEbHbAR/UCgYEA8E2VGyM6I7F/Adh01EDd3ZXganDiawy20y1jOq7A8iqwUwx/Uo7hU1g6BuJxULJjHLBr28cuzLli2MBmQt1d0DkCTH+fKhjuqTg5xUNAER/xapii8gX9Ru/qlnq4LoNDs8cwffO5VZzUHpIyZ2djk2f1nzTk2jU/eBpi14hkiv0CgYA7bcWbUBaI+jmx4tRPpNrzMgA75Z6rutPBzyevuolwMolOJaPK8/tWI7DsUyTCyH+lYL8HwHShshTuAU+lIMagoxLIs7HY2zGZMJJgdce1dy4tGD+kqJA/n9Ch9mw6ApeqQwPePoGudVlKmdJ492a2xuDi7ZqYFqWgE/1Fql0MhQKBgBTGgFeiSgp/8/0S27bmDMh1hrttGwYqSTwktmmCZkgJjMkBUZivpadw/0NNHILfiVYXopuTsuYcOQGIUM8DGHNCGoLGndoj4a5boBL/53ATlOmkXKIUBnu1jlCIN5g93QKT/AtHoQRX1Lb36Mer1p4N5Nxo1EhRRjV6AoWj0x15AoGAC6Vo0DDcnu7NDFM21Z4F/9TAakp8jWxqFzfcFMDO7VtjkGx0YftbXzXf7uKrAyjgu+OMxKTdS3xgPfYocxptMJg2QJUlZEMdOr4g8LTv/9YbqAfk9uNpeKmYKT7kjrKWMbgJZqBu1hVr6JldKEKPkzhJuWFmd5csIWEvlvFcoKc=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhHq8QGLOd1MqCdZXSfGrZmDMpxw2RgThNMXLv6alD3sfrlgLIZQ7fwhPlIN67gnNqa5uCbbyuNM3kq5ZyYR9jl0HItpyxR+oizWwZEn24y6UkmguHUvGSsnb0//pcN5q077xvUKkOYGOLl+rLRLmApVKlSpEaSXiKYT+NhKPPpjkCWlvrkdt7+BbDh9c86nwsshuY6NiY2bTQOjCm6HN8SzgjWzAv2/tHqNo6D4ghAbIuwwhTh1eLU77R3Oyqb4QJ8PNNrYT0FC9IT94pFvewEtGL4NW7py3twrU8pfNLQjwfxhwGFsfd3Dpq80+XTLSj2Pmf4o2I7/ItZfWHqd+iQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "https://53477x7268.wocp.fun/payed/notify";  // 见配置文件

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html"; // 见配置文件

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {
        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
